/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.command.webservice
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import multimedia.control.business.WebserviceDelegate;
	import multimedia.control.event.webservice.GetNewsRSSEvent;
	import multimedia.model.ModelLocator;
	import multimedia.model.vo.NewsRSSItemVO;
	import multimedia.util.LogUtil;
	
	import mx.collections.ArrayCollection;
	import mx.logging.ILogger;
	import mx.rpc.IResponder;
	
	/**
	 * 	GetNewsRSSCommand
	 * 
	 *	@author jemden
	 *	@version 1.0
	 */
	public class GetNewsRSSCommand extends SequenceCommand implements ICommand, IResponder
	{
		private var model:ModelLocator;
		
		private var e:GetNewsRSSEvent;
		
		private static const LOG:ILogger = LogUtil.getLogger(GetNewsRSSCommand);
		
		override public function execute(event:CairngormEvent):void
		{
			this.model = ModelLocator.getInstance();
			this.e = GetNewsRSSEvent(event);
			this.nextEvent = e.nextEvent;

			model.log = "GetNewsRSSCommand";
			LOG.debug("<<< Retrieve NewsRSS >>>");
			
			var service:WebserviceDelegate = new WebserviceDelegate(this);
			service.getNewsRSS(e.apiKey, e.playerId);
		}
		
		public function result(data:Object):void
		{	
			model.log = "GetNewsRSSCommand result";
			LOG.debug("<<< News RSS retrieved >>>");
			
			var items:XMLList = data.result..item;
			
			var currentNewsRSSItems:ArrayCollection = new ArrayCollection();
			
			var item:Object;
			var newsRSSItem:NewsRSSItemVO;
			for each (item in items)
			{
				newsRSSItem = new NewsRSSItemVO();
				newsRSSItem.title = item.title.toString();
				
				currentNewsRSSItems.addItem(newsRSSItem);
				model.log += "\n"+ newsRSSItem;
			}
			
			model.currentNewsRSSItems = currentNewsRSSItems.toArray();
			
			executeNextCommand();
		}
		
		public function fault(info:Object):void
		{
			model.log = "GetNewsRSSCommand Error";
			LOG.error("<<< ERROR when trying to retrieve NewsRSS ({0})", info);
			executeNextCommand();
		}
	}
}